import requests
from base import oss_client
import io

from service import service_namespace

def download_object_and_upload_to_oss(object_url,chapter_id,user_id, type):
    if object_url is None:
        return False, None, None

    bucket_name = service_namespace.get_buket_name(chapter_id=chapter_id, user_id=user_id, type=type)
    file_suffix = ''
    if type == service_namespace.SHORT_VIDEOS_BUCKET_NAME or type == service_namespace.VIDEOS_BUCKET_NAME:
        file_suffix = service_namespace.V_MP4
    elif type == service_namespace.AUDIOS_BUCKET_NAME:
        file_suffix = service_namespace.V_MP3
    elif type == service_namespace.IMAGES_BUCKET_NAME:
        file_suffix = service_namespace.I_PNG

    file_name = service_namespace.get_file_name(chapter_id=chapter_id, user_id=user_id, file_suffix=file_suffix)
    bytes_content = requests.get(object_url).content
    oss_client.upload_object(file_name=file_name, bucket_name=bucket_name, data=io.BytesIO(bytes_content), length=len(bytes_content))
    return True, file_name, bucket_name

def upload_file_to_oss(chapter_id ,user_id, file_path, type):
    if chapter_id is None or user_id is None or file_path is None or type is None:
        return False, None, None
    bucket_name= service_namespace.get_buket_name(chapter_id=chapter_id, user_id=user_id, type=type)
    file_suffix = ''
    if type == service_namespace.SHORT_VIDEOS_BUCKET_NAME or type == service_namespace.VIDEOS_BUCKET_NAME:
        file_suffix = service_namespace.V_MP4
    elif type == service_namespace.AUDIOS_BUCKET_NAME:
        file_suffix = service_namespace.V_MP3
    elif type == service_namespace.IMAGES_BUCKET_NAME:
        file_suffix = service_namespace.I_PNG

    file_name= service_namespace.get_file_name(chapter_id=chapter_id, user_id=user_id, file_suffix=file_suffix)
    oss_client.upload_file(file_name=file_name, bucket_name=bucket_name, file_path=file_path)
    return True, file_name, bucket_name



